package org.visionlibrary.image.filters.bluring;

import java.awt.Point;

import org.visionlibrary.image.model.ConvolutionKernel;


public class Gaussian {
	protected int size = 5;
	protected float sigma = 50.0f;
	protected Point origin = new Point(1, 1);

	protected float gaussian(float sigma, int x, int y) {
		float g = 0;
		float sigmasqr = sigma * sigma;
		float func = 1f / (2f * (float) Math.PI * sigmasqr);

		for (float ySubPixel = y - 0.5f; ySubPixel < y + 0.55f; ySubPixel += 0.1f) {
			for (float xSubPixel = x - 0.5f; xSubPixel < x + 0.55f; xSubPixel += 0.1f) {
				g += (float) ((func * Math.pow(Math.E,
						-(xSubPixel * xSubPixel + ySubPixel * ySubPixel)
								/ (2 * sigmasqr))));
			}
		}
		return g;
	}

	public ConvolutionKernel getFilterKernel() {
		float[] values = new float[size * size];

		// we generate the kernel
		for (int j = 0; j < size; ++j) {
			for (int i = 0; i < size; ++i) {
				values[j * size + i] = gaussian(sigma, i - (size / 2), j
						- (size / 2));
			}
		}

		// we must compute a divider
		float sum = 0;
		for (int j = 0; j < size; ++j) {
			for (int i = 0; i < size; ++i) {
				sum += values[j * size + i];
			}
		}

		return new ConvolutionKernel(size, size, values, sum, new Point(size / 2,
				size / 2));
	}

	public Point getOrigin() {
		return origin;
	}

	public float getSigma() {
		return sigma;
	}

	public int getSize() {
		return size;
	}

	public void setOrigin(Point origin) {
		this.origin = origin;
	}

	public void setSigma(float sigma) {
		this.sigma = sigma;
	}

	public void setSize(int size) {
		this.size = size;
	}
}
